package com.hsxc.cdz.monitor.bean.extend.server.message;

import com.hsxc.cdz.base.util.BytesUtil;
import com.hsxc.cdz.monitor.bean.extend.Msg;
import com.hsxc.cdz.monitor.bean.extend.server.content.ServerContent;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.util.ReferenceCountUtil;

public abstract class ServerMessage<T extends ServerContent> extends Msg<T> {
	
	/******************************************************/
	//常量
	// 服务端回复心跳
    public static final String RESPONSE_HEART = "52";
    // 服务端回复账单
 	public static final String RESPONSE_BILL = "55";
    // 服务端回复桩开始充电请求
 	public static final String RESPONSE_START = "56";
    // 服务端回复桩停止充电请求
 	public static final String RESPONSE_STOP = "57";
    // 服务端回桩挺住预约请求
 	public static final String RESPONSE_STOP_ORDER = "58";
    // 服务端回复刷卡请求
 	public static final String RESPONSE_CARD = "76";
    // 服务端回复注册或登录请求
 	public static final String RESPONSE_REGISTER = "A2";
    // 服务端回复登录退出请求
 	public static final String RESPONSE_UNLOAD = "A4";
	/**
	 * 从报文类中获得byte数组
	 * @return
	 */
	public byte[] getByte(){
		ByteBuf bytes = Unpooled.buffer(Msg.headLength);
		byte[] result;
		byte[] temp;
		System.out.println(1);
		temp  = BytesUtil.strToBinBytes(this.getIsOpen(),ISOPEN_LENGTH);
		bytes.writeBytes(temp);
		System.out.println(2);
		temp = BytesUtil.intToBytesDesc(this.getContentLength(),CLENGTH_LENGTH);
		bytes.writeBytes(temp);
		System.out.println(3);
		temp = BytesUtil.strToBinBytes(this.getIsRegister(), ISREGISTER_LENGTH);
		bytes.writeBytes(temp);
		System.out.println(4);
		temp = BytesUtil.strToBinBytes(this.getResponse(), RESPONSE_LENGTH);
		bytes.writeBytes(temp);
		System.out.println(5);
		temp = BytesUtil.strToBcdBytes(this.getOperator(),OPERATOR_LENGTH);
		bytes.writeBytes(temp);
		System.out.println(6);
		temp = BytesUtil.strToBcdBytes(this.getAddress(),ADDRESS_LENGTH);
		bytes.writeBytes(temp);
		System.out.println(7);
		temp = BytesUtil.strToBinBytes(this.getFrame(),FRAME_LENGTH);
		bytes.writeBytes(temp);
		System.out.println(8);
		temp = BytesUtil.strToBcdBytes(this.getLineNum(),LINE_LENGTH);
		bytes.writeBytes(temp);
		
		result = new byte[bytes.readableBytes()];
		bytes.readBytes(result);
		ReferenceCountUtil.release(bytes);
		return result;
	}
	
}
